package com.oocl.consumer.consumer;

import javax.jms.Connection;
import javax.jms.ConnectionFactory;
import javax.jms.Destination;
import javax.jms.JMSException;
import javax.jms.MessageConsumer;
import javax.jms.Session;

import org.apache.activemq.ActiveMQConnectionFactory;
import org.apache.activemq.command.ActiveMQQueue;

/**
 * 消费者
 * @author GANAB
 *
 */
public class BookConsumer {

	private static final String URL = "tcp://10.222.29.179:61616";
	private ConnectionFactory factory = null;
	private Destination queue = null;
	private Connection con = null;
	private Session session = null;
	private MessageConsumer consumer = null;
	public BookConsumer(String queueName, boolean arg0, int limit) {
		factory = new ActiveMQConnectionFactory(URL);
		queue = new ActiveMQQueue(queueName);
		try {
			con = factory.createConnection();
			con.start();
			session = con.createSession(arg0, Session.CLIENT_ACKNOWLEDGE);
			consumer = session.createConsumer(queue);
			consumer.setMessageListener(new BookMessageListener(limit));
		} catch (JMSException e) {
			e.printStackTrace();
		}
	}
	
	public void close() throws JMSException{
		if(consumer!=null){
			consumer.close();
		}
		if(session!=null){
			session.close();
		}
		if(con!=null){
			con.close();
		}
	}
}
